<?php
/*
 * 手机客户端 用户提交订单接口
 * 2013-12-03
 */
 
include "inc/public_header.php";

include "func/user.php";				//用户信息处理函数


$aids 			= $_REQUEST["aid"];				//商品id，多个用逗号分割
$name 			= $_REQUEST["name"];			//用户名
$province		= $_REQUEST["province"];		//省份
$city			= $_REQUEST["city"];			//市
$county			= $_REQUEST["county"];			//区
$detailAddress	= $_REQUEST["detailAddress"];	//地址
$postCode		= $_REQUEST["postCode"];		//邮编
$phone			= $_REQUEST["mobile"];			//手机号
$contact		= $_REQUEST["phone"];			//固定电话
$payType		= $_REQUEST["payType"];			//目前固定为1
$remarks		= $_REQUEST["remarks"];			//备注
$despatchType 	= $_REQUEST["despatchType"];	//物流方式  1上门自提  2物流配送 

if(!$despatchType){
	$despatchType = 2; //默认物流配送
}
if(!$aids){
	$aids = "0"; //放一个默认值
}

//获取用户信息
$uInfoArr = user_getInfoFromUUID();
if(!$uInfoArr){
	echo json_encode(array("success"=>"0","errCode"=>"0","msg"=>"没有获取到用户信息"));
	die;
}
$uid = $uInfoArr["uid"]; //用户信息主键

$reJson = array(); //构建返回值

$carriage = 0; //运费
if($province){
	//计算运费
	$sql = "select sum(aweight) as total_weight from auction where aid in (".$aids.")";
	$db->query($sql);
	$db->next_record();
	$tweight = $db->f("total_weight"); //总重量
	
	$sql="call pro_get_yunfei('".$tweight."','".$province."',@x)";
	$db->query($sql);
	$db->query("select @x");
	if($db->next_record()){
		$carriage = $db->f("@x");
	}	
}

//获取支付信息
$sql = "select pay_id,pay_name from set_payment where pay_id='".$payType."'";
$db->query($sql);
if($db->next_record()){
	$payName = $db->f("pay_name");
}else{
	$payName = "";
}
$sql = "select shipping_name from set_shipping where shipping_id='".$despatchType."'";
$db->query($sql);
if($db->next_record()){
	$shipping_name = $db->f("shipping_name");
}else{
	$shipping_name = "";
}

//拼装收货地址
$shipArea = $province;
if($city){
	$shipArea = $shipArea." ".$city;
}
if($county){
	$shipArea = $shipArea." ".$county;
}


//插入支付总表记录
$sql = "insert into auction_userwonpaid (uid,fare,pay_id,payment_typeid,payment,shipping_id,shipment,createtime,ship_addrid,ship_name,ship_province,ship_city,ship_district,ship_area,ship_addr,ship_zip,ship_tel,ship_mobile,yunfei,remark)
	values('".$uid."','".$carriage."','".$payType."','mchinapay','".$payName."','".$despatchType."','".$shipping_name."',now(),0,'".$name."','".$province."','".$city."','".$country."','".$shipArea."','".$detailAddress."','".$postCode."','".$contact."','".$phone."','".$carriage."','".$remarks."')";
$db->query($sql);

$paidId = $db->insert_id(); //主表主键

//查询拍品信息
$sql = "select
			a.aulbid,
			a.aid,
			b.acode,
			b.auction,
			b.endtime,
			date_add(b.endtime,interval 14 day) as limitime,
			a.bid,
			b.servicefee
		from
			auction_userlastbid a
			inner join auction b on b.aid = a.aid
		where 
			a.is_bid=1 
			and a.uid='".$uid."'
			and b.aid in (".$aids.")";
$db->query($sql);
$sumba = 0; //累计金额
$payCount = 0; //支付商品数量
while($db->next_record()){
	$payCount++;
	$bid_fee = $db->f("servicefee")*0.01*$db->f("bid");
	$bid_amount = $db->f("bid")+$bid_fee;
	$sumba = $sumba+$bid_amount;
	
	//插入子表记录
	$sql = "insert into auction_userwon(paidID,uid,aid,acode,auction,endtime,limittime,bid,bid_fee,bid_amount,aulbid) 
	values('".$paidId."','".$uid."','".$db->f("aid")."','".$db->f("acode")."','".$db->f("auction")."','".$db->f("endtime")."','".$db->f("limitime")."','".$db->f("bid")."','".$bid_fee."','".$bid_amount."','".$db->f("aulbid")."')";
	$db2->query($sql);
	
	//回写竞拍结果
	$sql = "update auction_userlastbid set wonID='".$db2->insert_id()."' where aulbid='".$db->f("aulbid")."'";
	$db2->query($sql);
}
//回写主表
$sql = "update auction_userwonpaid set bids_cost='".$sumba."',total_amount='".($sumba+$carriage)."',final_amount='".($sumba+$carriage)."',acttime=now() where paidID='".$paidId."'";
$db->query($sql);

//写入支付单日志
$sql = "insert into auction_action(paidid,action_user,action_note,log_time)
	values('".$paidId."','".$uInfoArr["mobile"]."','会员手机创建',now())
";
$db->query($sql);

$sum1 = $sumba+$carriage;

$reJson = array(
	 "success"=>"1"
	,"orderTime"=>date('Y-m-d H:i:s')
	,"orderId"=>"$paidId"
	,"address"=>$shipArea." ".$detailAddress
	,"payType"=>$payType
	,"payName"=>$payName
	,"goodsCount"=>"$payCount"
	,"remarks"=>$remarks
	,"pay"=>"$sum1"
);
echo json_encode($reJson);
 ?>